package com.ddone.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddone.mapper.SysLoginLogMapper;
import com.ddone.model.auth.SysLoginLog;
import com.ddone.model.vo.SysLoginLogQueryVo;
import com.ddone.service.SysLoginLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author ddone
 * @date 2024/4/2-10:15
 */
@Service
public class SysLoginLogServiceImpl extends ServiceImpl<SysLoginLogMapper, SysLoginLog> implements SysLoginLogService {
    @Resource
    private SysLoginLogMapper sysLoginLogMapper;

    @Override
    public IPage<SysLoginLog> findPage(Page<SysLoginLog> pageParam, SysLoginLogQueryVo sysLoginLogQueryVo) {
        LambdaQueryWrapper<SysLoginLog> queryWrapper = new LambdaQueryWrapper<>();
        String createTimeBegin = sysLoginLogQueryVo.getCreateTimeBegin();
        String createTimeEnd = sysLoginLogQueryVo.getCreateTimeEnd();
        String username = sysLoginLogQueryVo.getUsername();
        queryWrapper.like(StringUtils.isNotBlank(username), SysLoginLog::getUsername, username)
                .ge(Objects.nonNull(createTimeBegin), SysLoginLog::getCreateTime, createTimeBegin)
                .le(Objects.nonNull(createTimeEnd), SysLoginLog::getCreateTime, createTimeEnd)
                .orderByDesc(SysLoginLog::getId);
        return sysLoginLogMapper.selectPage(pageParam,queryWrapper);
    }

    @Override
    public void recordLoginLog(String username, String ipAddress, Integer status, String msg) {
        SysLoginLog loginLog = SysLoginLog.builder()
                .username(username)
                .ipaddr(ipAddress)
                .status(status)
                .msg(msg)
                .build();
        sysLoginLogMapper.insert(loginLog);
    }
}
